Write Initialize Smart Contract & Join DAO Functions
In this step, we will be writing the Initialize and Join DAO functions!
Proceed to head over to the //Step C - Write Initialize Smart Contract
and Join DAO Logic comment and overwrite the existing initializeAndJoinDAO function with the following code snippet.
const initializeAndJoinDAO = async () => {
//Step C - Write Initialize Smart Contract and Join DAO Logic
try {
const accounts = await provider?.request({
method: MethodsBase.ACCOUNTS,
});
if (!accounts) throw new Error("No accounts");
const account = accounts?.tDVW?.[0];
if (!account) throw new Error("No account");
if (!initialized) {
await DAOContract?.callSendMethod("Initialize", account, {});
setInitialized(true);
alert("DAO Contract Successfully Initialized");
}
await DAOContract?.callSendMethod("JoinDAO", account, account);
setJoinedDAO(true);
alert("Successfully Joined DAO");
} catch (error) {
console.error(error, "====error");
}
};
In this function, we will first fetch our wallet account using the configured Portkey wallet provider!
Next, we will invoke the Initialize method from our deployed smart contract to initialize the DAO smart contract and update the state variable to true once the contract has been initialized!
Next, we will invoke the JoinDAO method parsing in our account wallet address as an input parameter as defined from our smart contract. Similarly, we will update the state variable to true and prompt an alert that the user has successfully joined the DAO!
We will then wrap the InitializeAndJoinDAO function in the JoinDAO button as shown below which triggers both the Initialize and JoinDAO upon clicking on "Join DAO"!
Preview of Join DAO button
In the next step, we will be writing the **Create Proposal** function!